<?php 
abstract class TaksasiDAO{
    protected $db;
    
    public function TaksasiDAO(){
        require 'config/db_config.php';
        
        $this->db = new ezSQL_mysql($dbUser, $dbPass, $dbName, $dbHost);
    }
     
    abstract public function get();
}

class MonthDistributionDAO extends TaksasiDAO{
    private $tahun;
    
    public function setTahun($t){
        $this->tahun = $t;
    }
    
    public function get(){
        $q = "
            SELECT bulan, dist
            FROM taksasi_dist
            WHERE tahun='".$this->tahun."'
            ORDER BY bulan";
            
        $data = $this->db->get_results($q, ARRAY_A);
        
        $result = array();
        if( !is_array($data) ){
            $result = array_fill(1,12, 1/12);
        }
        else{
            foreach($data as $d){
                $result[$d['bulan']] = $d['dist'];
            }
        }
        
        return $result;
    }
}

class TaksasiConfigDAO extends TaksasiDAO{
    public function get(){
        $q = "
            SELECT name, value
            FROM taksasi_config";
            
        $data = $this->db->get_results($q, ARRAY_A);
        
        $result = array();
        foreach($data as $d){
            $result[$d['name']] = $d['value'];
        }
       
        
        return $result;
    }
    
    public function getCorrectionFactorConfig($phase){
        $config = $this->get();
        
        $order = array(
            'bagging'   => $config['correction_factor_bagging'], 
            'polinasi'  => $config['correction_factor_bagging'] * 
                           $config['correction_factor_polinasi'],
            'panen'     => $config['correction_factor_bagging'] * 
                           $config['correction_factor_polinasi'] * 
                           $config['correction_factor_panen'],
            'saleable'  => $config['correction_factor_bagging'] * 
                           $config['correction_factor_polinasi'] * 
                           $config['correction_factor_panen'] *
                           $config['correction_factor_saleable'],
        );
        
        return $order[$phase];
    }
}

class BphDAO extends TaksasiDAO{
    public function get(){
        $q = "
            SELECT tm, bph
            FROM taksasi_bph
            ORDER BY tm";
            
        $data = $this->db->get_results($q, ARRAY_A);
        
        $result = array();
        foreach($data as $d){
            $result[$d['tm']] = $d['bph'];
        }
        
        return $result;
    }
}

class BjrDAO extends TaksasiDAO{
    public function get(){
        $q = "
            SELECT tm, bjr
            FROM taksasi_bjr
            ORDER BY tm";
            
        $data = $this->db->get_results($q, ARRAY_A);
        
        $result = array();
        foreach($data as $d){
            $result[$d['tm']] = $d['bjr'];
        }
        
        return $result;
    }
}

class BpjDAO extends TaksasiDAO{
    public function get(){
        $q = "
            SELECT tm, bpj
            FROM taksasi_bpj
            ORDER BY tm";
            
        $data = $this->db->get_results($q, ARRAY_A);
        
        $result = array();
        foreach($data as $d){
            $result[$d['tm']] = $d['bpj'];
        }
        
        return $result;
    }
}


class TreeCompositionDAO extends TaksasiDAO{
    public function get(){
        $q = "
            SELECT 
                 COUNT(no_individu) as jumlah,
                 DATE_FORMAT(NOW(), '%Y') - DATE_FORMAT(tgl_penanaman, '%Y') - (DATE_FORMAT(NOW(), '00-%m-%d') < DATE_FORMAT(tgl_penanaman, '00-%m-%d'))+usia_saat_tanam AS tm
            FROM biodata_tanaman
            WHERE no_individu IN (SELECT no_individu FROM pancang)
            GROUP BY tm
            ";
            
        $data = $this->db->get_results($q, ARRAY_A);
        
        $result = array();
        foreach($data as $d){
            $result[$d['tm']] = $d['jumlah'];
        }
        
        return $result;
    }
}
?>